/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.db;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.sql.*;

import blueprint4j.utils.*;


/**
 * This will offer select options from a select in the database
 */
public class FieldLongSelectDB extends FieldLong implements BindStringSelectInterface{

	private String sql_select = null,display_field=null,options_field=null;
	private VectorLong options = new VectorLong();
	private List<String> display = new ArrayList<String>();

	/**
	 * Build a sql select
	 * The display values will come from 
	 */
	public FieldLongSelectDB( String p_name, int p_field_type ,String p_description,String sql_select,String display_field,String options_field,Entity entity ) {
		super( p_name,p_field_type,p_description,entity );
		this.sql_select = sql_select;
		this.display_field =display_field;
		this.options_field = options_field;
	}

	public Field newInstance(Entity entity) {
		return new FieldLongSelectDB(getName(),getFieldType(),getDescription(),sql_select,display_field,options_field,entity);
	}

	/**
	 * This build the sql select and populates the values
	 */
	public void refresh()
	throws BindException {
		try {
			Statement smt = getEntity().getConnection().createStatement();
			options = new VectorLong();
			display = new ArrayList<String>();
			for (ResultSet rs = smt.executeQuery(sql_select);rs.next();) {
				options.add(new Long(rs.getLong(options_field)));
				display.add(rs.getString(display_field));
			}
			smt.close();
		} catch (Exception exception) {
			throw new BindException(exception);
		}
	}

	public void setAsString(String value) {
		if (value == null) {
			set(null);
		} else {
			set(new Long(value));
		}
	}
	
	public void set(Long value){
		if (value != null && !options.contains(value)) {
			Log.debug.out("FieldLongSelectDB set to a value ["+value+"] which is not conatined in the options list ["+options+"]","");
		}
		setObject(value);
	}
	
	public String getAsString(){
		if (get() == null){
			return null;
		}
		return get().toString();
	}

	public String[] getOptions() {
		return options.toStringArray();
	}

	public String[] getDisplays() {
		return display.toArray(new String[0]);
	}

	public String getDisplay() {
		if (get() != null && display.size() > options.indexOf(get())) {
			return display.get(options.indexOf(get()));
		}
		return null;
	}

}
